package com.google.android.apps.wakemewhere.mapsearch;

import com.google.android.apps.wakemewhere.IntentExtras;
import com.google.android.maps.GeoPoint;

import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

public class SearchResult implements Parcelable, Comparable<SearchResult> {
	private final String name;
	private final String address;
	private final int priority;
	private final GeoPoint location;
	
	public SearchResult(String name, String address, int priority, GeoPoint location) {
		this.name = name;
		this.address = address;
		this.priority = priority;
		this.location = location;
	}
	
	public String toString() {
		return name + " [" + address + "]";
	}
	
	public static final Parcelable.Creator<SearchResult> CREATOR = new Parcelable.Creator<SearchResult>() {
		public SearchResult createFromParcel(Parcel in) {
			String name = in.readString();
			String address = in.readString();
			int priority = in.readInt();
			int latitudeE6 = in.readInt();
			int longitudeE6 = in.readInt();
			GeoPoint location = new GeoPoint(latitudeE6, longitudeE6);
			return new SearchResult(name, address, priority, location);
		}

		public SearchResult[] newArray(int size) {
			return new SearchResult[size];
		}
	};
	
	public int describeContents() {
		return 0;
	}
	
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(name);
		dest.writeString(address);
		dest.writeInt(priority);
		dest.writeInt(location.getLatitudeE6());
		dest.writeInt(location.getLongitudeE6());
	}
	
	public String getAddress() {
		return address;
	}
	
	public String getName() {
		return name;
	}
	
	public int compareTo(SearchResult another) {
		return Integer.valueOf(priority).compareTo(another.priority);
	}

	public void writeOnto(Intent intent) {
    	intent.putExtra(IntentExtras.ADDRESS, getAddress());
    	intent.putExtra(IntentExtras.LATITUDE, location.getLatitudeE6());		
    	intent.putExtra(IntentExtras.LONGITUDE, location.getLongitudeE6());
	}
}